// coursedetail.js
import api from "../../../utils/api"
import util from "../../../utils/util"
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:"",
    isExpend:false,
    isTalk:true,
    course:{},
    talkList:[],
    talkText:"",
    isSaved:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    util.isLogin();
    console.log(options);
    this.setData({
      id: options.id
    });
    //获取课程详细信息
    this.getCourseDetail({});
    //获取课程的评论信息
    this.getTalks();

    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * 展开讲师信息
   */
  expendTeacherInfo:function(){
  this.setData({
    isExpend: !this.data.isExpend
  });
  },
  /**
   * 评论、推荐视频tab页特效
   */
  changeTalkTab:function(e){
    var index = e.target.dataset.index;
    if(index == 1){
      this.setData({
        isTalk:true
      });
    }else{
      this.setData({
        isTalk: false
      });
    }
  },
  /**
   * 获取课程信息详情
   */
  getCourseDetail:function(){
    var that = this;
    wx.request({
      url: api.getCourseById,
      header:{
        "content-type": "application/x-www-form-urlencoded"
      },
      data:{
        // id:that.data.id
        id:1
      },
      method:"POST",
      success:function(response){
        console.log("查询课程信息成功！");
        console.log(response.data.data[0]);
        that.setData({
          course: response.data.data[0]
        });
      }
    })
  },
  /**
   * 获取评论信息
   */
  getTalks:function(){
    console.log("获取评论信息");
    debugger;
    var that = this;
    wx.request({
      url: api.getCourseTalks,
      data:{
        id:that.data.id
      },
      header:{
        "content-type": "application/x-www-form-urlencoded"
      },
      method:"POST",
      success:function(response){
        console.log("获取评论信息成功！");
        console.log(response.data);
        that.setData({
          talkList:response.data.data
        });
      }
    })
  },
  /**
   * 发表评论
   */
  talk: function () {
    console.log("发表评论");
    var that = this;
    debugger;
    var talkText = this.data.talkText;
    this.data.talkList.push({
      content: this.data.talkText,
      id: app.globalData.openid,
      head: app.globalData.userHead,
      time: util.formatTime(new Date()),
      name: app.globalData.userName
    });
    that.setData({
      talkList: that.data.talkList,
      talkText: ""
    });
    //发送请求
    wx.request({
      url: api.addCourseComment,
      data:{
        course_id:this.data.id,
        u_id:app.globalData.openid,
        comment: talkText,
        head: app.globalData.userHead,
        name: app.globalData.userName,
        time: util.formatTime(new Date())
      },
      header:{
        "content-type": "application/x-www-form-urlencoded"
      },
      method:"POST",
      success:function(response){
        console.log("评论课程成功！");
        console.log(response.data);
      }
    })
  },
  /**
   * 评论内容输入
   */
  talkTextInput:function(e){
    this.setData({
      talkText:e.detail.value
    });

  },
/**
 * 收藏
 */
save:function(){
  var that = this;
  wx.request({
    url: api.coursetrove,
    data:{
      id:app.globalData.openid,
      course:this.data.id
    },
    header:{
      "content-type": "application/x-www-form-urlencoded"
    },
    method:"POST",
    success:function(response){
      console.log("收藏课程成功！");
      console.log(response.data);
      var flag = response.data.infor;
      if (flag == "收藏课程成功"){
        that.setData({
          isSaved: true
        });
      }else{
        that.setData({
          isSaved: false
        });
      }
      
    }
  })
}
})